package cn.jsj.cxsys.controller;

import org.springframework.stereotype.*;
import cn.jsj.cxsys.service.*;
import org.springframework.beans.factory.annotation.*;
import cn.jsj.cxsys.pojo.*;
import org.springframework.web.bind.annotation.*;
import java.util.*;
import cn.jsj.cxsys.controller.packageClass.*;

@ResponseBody
@CrossOrigin
@Controller
public class UserController
{
    @Autowired
    private UserService userService;

    @RequestMapping(value = { "login" }, params = { "userName", "password" }, method = { RequestMethod.POST })
    public String login(@RequestParam("userName") final String userName, @RequestParam("password") final String password) {
        return this.userService.login(userName, password);
    }

    @RequestMapping(value = { "login" }, params = { "user" }, method = { RequestMethod.POST })
    public String login(@RequestBody final User user) {
        return this.userService.login(user);
    }

    @RequestMapping(value = { "registerStudent" }, method = { RequestMethod.POST })
    public String registerStudent(@RequestBody final User user) {
        if (user == null || "计算机学院教职工".equals(user.getClassName())) {
            return "paramLess";
        }
        return this.userService.RegisterStudent(user);
    }

    @RequestMapping(value = { "getClassName" }, method = { RequestMethod.POST })
    public List<String> getClassList() {
        return (List<String>)this.userService.getAllClassName();
    }

    @RequestMapping(value = { "getInformation" }, params = { "account", "password" }, method = { RequestMethod.POST })
    public User getPerpson(@RequestParam("account") final String account, @RequestParam("password") final String password) {
        return this.userService.getUserByAccount(account, password);
    }

    @RequestMapping(value = { "superGetUsers" }, params = { "account", "password", "page", "rows" })
    public EasyUIPages<User> superGetUsers(@RequestParam("account") final String superAccount, @RequestParam("password") final String password, @RequestParam("page") final int pageNum, @RequestParam("rows") final int pageSize) {
        final int allNum = this.userService.getUserNumber();
        final List<User> tep = (List<User>)this.userService.findAllUsers(superAccount, password, pageNum, Integer.MAX_VALUE);
        return new EasyUIPages<User>(allNum, tep);
    }

    @RequestMapping(value = { "superExcelRegister" }, method = { RequestMethod.POST })
    public String excelImportStudent(@RequestBody final List<User> storage) {
        if (storage == null || storage.size() == 0) {
            return "error";
        }
        final StringBuffer result = new StringBuffer("");
        for (final User i : storage) {
            i.setPassword("A000000000A");
            final String str = this.registerStudent(i);
            if (!"success".equals(str)) {
                result.append(i.getName() + ",");
            }
        }
        if (result != null && result.length() > 0) {
            result.deleteCharAt(result.length() - 1);
            result.append(" 注册失败请检查个人信息");
            return result.toString();
        }
        return "所有用户注册成功";
    }

    @RequestMapping(value = { "superExcelRegisterTeacher" }, method = { RequestMethod.POST })
    public String excelImportTeachers(@RequestBody final List<User> storage) {
        if (storage == null || storage.size() == 0) {
            return "error";
        }
        final StringBuffer result = new StringBuffer("");
        for (final User i : storage) {
            final String tt = i.getAccount();
            if (i.getPassword() == null || i.getPassword().trim().length() == 0) {
                tt.substring(0, 6);
                i.setPassword("AA" + tt);
            }
            final String str = this.userService.RegisterTeacher(i);
            if (!"success".equals(str)) {
                result.append(i.getName() + ",");
            }
        }
        if (result != null && result.length() > 0) {
            result.deleteCharAt(result.length() - 1);
            result.append(" 注册失败请检查个人信息");
            return result.toString();
        }
        return "所有教师注册成功";
    }

    @RequestMapping(value = { "superChangeUser" }, method = { RequestMethod.POST })
    public String superChangeUsers(@RequestBody final Super_UserList su) {
        if (su == null) {
            return "paramLess";
        }
        return this.userService.updateUserInformation(su.getAccount(), su.getPassword(), (List)su.getStorage());
    }

    @RequestMapping(value = { "superGetTeacher" }, method = { RequestMethod.POST }, params = { "superAccount", "password", "page", "rows" })
    public EasyUIPages<User> getTeachers(@RequestParam("superAccount") final String superAccount, @RequestParam("password") final String password, @RequestParam("page") final int page, @RequestParam("rows") final int rows) {
        final int allnum = this.userService.countTeachers();
        final List<User> userStorage = (List<User>)this.userService.superGetAllTeacher(superAccount, password, page, rows);
        return new EasyUIPages<User>(allnum, userStorage);
    }

    @RequestMapping(value = { "resetPassword" }, method = { RequestMethod.POST }, params = { "account", "password", "newPassword" })
    public String userRefreshPassword(@RequestParam("account") final String account, @RequestParam("password") final String password, @RequestParam("newPassword") final String newPassword) {
        return this.userService.changePassword(account, password, newPassword);
    }

    @RequestMapping(value = { "status" }, method = { RequestMethod.POST }, params = { "account", "password" })
    public String userStatus(@RequestParam("account") final String account, @RequestParam("password") final String password) {
        return this.userService.checkStudentState(this.userService.getUserByAccount(account, password));
    }
}